package com.gupao.vip.nio.buffer;

import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * I/O映射缓冲区
 * @创建人 majingang
 * @联系方式 aoyouxingkong08@163.com
 * @创建日期 2020-04-15
 * @描述
 */
public class MapperBufferDemo {
    public static void main(String[] args) throws Exception{
        RandomAccessFile randomAccessFile = new RandomAccessFile("d:\\LibAntiPrtSc_ERROR-copy.log", "rw");
        FileChannel fileChannel = randomAccessFile.getChannel();

        /**
         * 把缓冲区跟文件系统进行一个映射关联
         * 只要操作缓冲区里面的内容，文件内容也会跟着变化
         */
        MappedByteBuffer byteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, 1024);
        byteBuffer.put(0, (byte)97);
        byteBuffer.put(1023, (byte)122);
        randomAccessFile.close();
    }
}
